import * as THREE from 'three'

export const createFanShape = (radius, color) => {
  const fanShape = new THREE.Shape()
  fanShape.moveTo(0, 0)
  fanShape.lineTo(radius * Math.cos(-Math.PI / 4), radius * Math.sin(-Math.PI / 4))
  fanShape.absarc(0, 0, radius, -Math.PI / 4, Math.PI / 4, false)
  fanShape.lineTo(0, 0)
  const geometry = new THREE.ShapeGeometry(fanShape)
  const material = createFanMaterial(color)
  material.uniforms.radius.value = radius
  return new THREE.Mesh(geometry, material)
}
export const createFanMaterial = (color) => {
  return new THREE.ShaderMaterial({
    uniforms: {
      color1: { value: new THREE.Color(color) },
      color2: { value: new THREE.Color(0x000000) },
      radius: { value: 1.0 }, // 添加半径uniform
    },
    vertexShader: /* glsl */ `
      varying vec2 vPosition;
      void main() {
        vPosition = position.xy;
        gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
      }
    `,
    fragmentShader: /* glsl */ `
      uniform vec3 color1;
      uniform vec3 color2;
      uniform float radius;
      varying vec2 vPosition;

      void main() {
        float distance = length(vPosition);
        float intensity = 1.0 - smoothstep(0.0, radius, distance);
        vec3 finalColor = mix(color2, color1, intensity * 2.0);
        gl_FragColor = vec4(finalColor, intensity * 0.8);
      }
    `,
    transparent: true,
    side: THREE.DoubleSide,
  })
}
